<?php

namespace App\Jobs;

use App\Http\Controllers\Admin\QingPingController;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use DB;

/* 记录登录日志任务 */

class LoginLogJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data_login_log_job;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data_login_log_job)
    {
        $this->data_login_log_job = $data_login_log_job;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(2);//有必要睡眠2秒，防止频繁请求获取ip api出错
        //将登录日志存到数据库
        DB::table('login_log')->insert([
            'ip'         => $this->data_login_log_job['ip'],
            'addr'       => \App\Libs\Helper::getAddrByIp($this->data_login_log_job['ip']),
            'manager_id' => $this->data_login_log_job['manager_id'],
            'realname'   => $this->data_login_log_job['realname'],
        ]);
        $qp = new QingPingController();
        $qp->delEnvInfo();
    }
}
